package TamirsGui;

import java.util.Vector;

import TamirsGui.GuiMessage.MessageAuthorization;
import TamirsGui.GuiMessage.MessageSubType;
import TamirsGui.GuiMessage.MessageType;

public class MainWindowMsg{

	public MainWindowMsg(){
		
	}
	public MainWindowMsg(String messageFromServer) {
		this.parseMessage(messageFromServer);
	}
	/**
	 * 
	 * @param messageFromServer the received messaged from the server. we know that the message is for the main gui window.
	 * here we parse the message for the format of the dispatch thread.
	 * @return vector of messages for the main window which the dispatch thread knows how to handle. 
	 */
	public Vector<GuiMessage> parseMessage(String messageFromServer) {
		String[] splitedMessage ;
		if(messageFromServer.substring(0, 8).equalsIgnoreCase("postList")){
			splitedMessage = messageFromServer.split((char)1+"");
		}
		else
			splitedMessage = messageFromServer.split("\n");
		int messageLength = splitedMessage.length;
		String [] messageWithoutTopic = new String[messageLength -1];
		System.arraycopy(splitedMessage, 1, messageWithoutTopic, 0, messageLength -1);
		String messageTopic = splitedMessage[0]; 
		return this.getMessageInfo(messageTopic , messageWithoutTopic);
	}

	private Vector<GuiMessage> getMessageInfo(String messageTopic,
			String[] messageWithoutTopic) {
		Vector<GuiMessage> serverMessages = new  Vector<GuiMessage>();
		if(messageTopic.equalsIgnoreCase("login")){
			buildLoginMsg(serverMessages,messageWithoutTopic,messageTopic);
		}
		if(messageTopic.equalsIgnoreCase("register")){
			buildRegisterMsg(serverMessages,messageWithoutTopic,messageTopic);
		}
		if(messageTopic.equalsIgnoreCase("ThreadList")){
			buildThreadListMsg(serverMessages,messageWithoutTopic,messageTopic);
		}
		if(messageTopic.equalsIgnoreCase("postList")){
			buildPostListMsg(serverMessages,messageWithoutTopic,messageTopic);
		}
		if(messageTopic.equalsIgnoreCase("addedSubForum")){
			buildaddedSubForumMsg(serverMessages,messageWithoutTopic,messageTopic);
		}
		if(messageTopic.equalsIgnoreCase("CandidateList")){
			buildCandidateListMsg(serverMessages,messageWithoutTopic,messageTopic);
		}
		if(messageTopic.equalsIgnoreCase("ModeratorAdd")){
			buildModeratorAdd(serverMessages,messageWithoutTopic,messageTopic);
		}
		if(messageTopic.equalsIgnoreCase("MsgDeleted")){
			buildMsgDeletedAck(serverMessages,messageWithoutTopic,messageTopic);
		}
		if(messageTopic.equalsIgnoreCase("SubForumDeleted")){
			BuildSubForumDeletedAck(serverMessages,messageWithoutTopic,messageTopic);
		}
		if(messageTopic.equalsIgnoreCase("MsgEdited")){
			buildMsgEditedAck(serverMessages,messageWithoutTopic,messageTopic);
		}

		if(messageTopic.equalsIgnoreCase("ModeratorRemoved")){
			buildModeratorRemovedAck(serverMessages,messageWithoutTopic,messageTopic);
		}
		if(messageTopic.equalsIgnoreCase("ModeratorsList")){
			buildModeratorList(serverMessages,messageWithoutTopic,messageTopic);
		}
		return serverMessages;


	}

	private void buildModeratorList(Vector<GuiMessage> serverMessages,
		String[] messageWithoutTopic, String messageTopic) {
		String authorization = messageWithoutTopic[0]; 
		for(int i = 1 ; i < messageWithoutTopic.length -1; i++){
			String body = messageWithoutTopic[i];
			String title = messageTopic;
			MessageType type = MessageType.content;
			MessageSubType subType = MessageSubType.moderatorList;
			MessageAuthorization userAutoAuthorization = MessageAuthorization.valueOf(authorization);
			GuiMessage newGuiMessage = new GuiMessage(body, title, type, subType, userAutoAuthorization);
			serverMessages.add(newGuiMessage);
		}
	}

	private void buildModeratorRemovedAck(Vector<GuiMessage> serverMessages,
			String[] messageWithoutTopic, String messageTopic) {
		buildAllAckMesages(serverMessages, messageWithoutTopic,messageTopic, MessageSubType.moderatorRemoved);			
	}

	private void buildAllAckMesages(Vector<GuiMessage> serverMessages,
			String[] messageWithoutTopic,String messageTopic,MessageSubType subType) {
		String authorization = messageWithoutTopic[0];
		String title = messageTopic;
		String body = "";
		for(int i = 1 ; i < messageWithoutTopic.length-1;i++)
			body+=messageWithoutTopic[i]+"\n";
		MessageType type = MessageType.ack;
		MessageAuthorization userAutoAuthorization = MessageAuthorization.valueOf(authorization);
		GuiMessage newGuiMessage = new GuiMessage(body, title, type, subType, userAutoAuthorization);
		serverMessages.add(newGuiMessage);		
	}

	private void buildMsgEditedAck(Vector<GuiMessage> serverMessages,
			String[] messageWithoutTopic, String messageTopic) {
		buildAllAckMesages(serverMessages, messageWithoutTopic,messageTopic,MessageSubType.msgEdited);	
		
	}

	private void BuildSubForumDeletedAck(Vector<GuiMessage> serverMessages,
			String[] messageWithoutTopic, String messageTopic) {
		buildAllAckMesages(serverMessages, messageWithoutTopic,messageTopic,MessageSubType.subForumDeleted);	
		
	}

	private void buildMsgDeletedAck(Vector<GuiMessage> serverMessages,
			String[] messageWithoutTopic, String messageTopic) {
		buildAllAckMesages(serverMessages, messageWithoutTopic,messageTopic,MessageSubType.msgDeleted);	
		
	}

	private void buildModeratorAdd(Vector<GuiMessage> serverMessages,
			String[] messageWithoutTopic, String messageTopic) {
		buildAllAckMesages(serverMessages, messageWithoutTopic,messageTopic,MessageSubType.moderatorAdded);	
		
	}

	private void buildCandidateListMsg(Vector<GuiMessage> serverMessages,
		String[] messageWithoutTopic, String messageTopic) {
		String authorization = messageWithoutTopic[0]; 
		for(int i = 1 ; i < messageWithoutTopic.length -1; i++){
			String body = messageWithoutTopic[i];
			String title = messageTopic;
			MessageType type = MessageType.content;
			MessageSubType subType = MessageSubType.candidateList;
			MessageAuthorization userAutoAuthorization = MessageAuthorization.valueOf(authorization);
			GuiMessage newGuiMessage = new GuiMessage(body, title, type, subType, userAutoAuthorization);
			serverMessages.add(newGuiMessage);
		}
		
	}

	private void buildaddedSubForumMsg(Vector<GuiMessage> serverMessages,
			String[] messageWithoutTopic, String messageTopic) {
		buildAllAckMesages(serverMessages, messageWithoutTopic,messageTopic,MessageSubType.subForumAdded);
		
		
	}

	//@TODO to chage the delimiter from \n
	private void buildPostListMsg(Vector<GuiMessage> serverMessages,
			String[] messageWithoutTopic, String messageTopic) {
			String authorization = messageWithoutTopic[0]; 
			String[] posts = new String [messageWithoutTopic.length -1];
			System.arraycopy(messageWithoutTopic, 1, posts,0, messageWithoutTopic.length -1);
			System.out.println(posts.length);
			if(posts.length == 1)
				buildEmptyPostList(serverMessages,authorization);
//			String[] msgTitleAndBody = posts.split("/n");
			for(int i = 0 ; i < posts.length - 1; i++){
				System.out.println(posts[i]);
				String postMsg = posts[i];
				String[] msg = postMsg.split((char)2+"");
				String messageHeader = msg[0];
				String[] messageInfo = messageHeader.split(":");
				String messageId = messageInfo[0];
				String title = messageInfo[1];
				authorization = msg[1];
				String body = msg[2];		
				System.out.println(title+" "+body+" "+authorization);
				MessageType type = MessageType.content;
				MessageSubType subType = MessageSubType.postMsg;
				MessageAuthorization userAutoAuthorization = MessageAuthorization.valueOf(authorization);
				GuiMessage newGuiMessage = new GuiMessage(body, title, type, subType, userAutoAuthorization,messageId);
				serverMessages.add(newGuiMessage);
			}
	}

	private void buildEmptyPostList(Vector<GuiMessage> serverMessages, String authorization) {
		String body = "";
		String title = "";
		MessageType type = MessageType.content;
		MessageSubType subType = MessageSubType.postMsg;
		MessageAuthorization userAutoAuthorization = MessageAuthorization.valueOf(authorization);
		GuiMessage newGuiMessage = new GuiMessage(body, title, type, subType, userAutoAuthorization);
		serverMessages.add(newGuiMessage);

		
	}
	private void buildThreadListMsg(Vector<GuiMessage> serverMessages,
			String[] messageWithoutTopic, String messageTopic) {
		String authorization = messageWithoutTopic[0]; 
		for(int i = 1 ; i < messageWithoutTopic.length -1; i++){
			String body = "";
			String messageHeader = messageWithoutTopic[i];
			String[] messageInfo = messageHeader.split(":");
			String messageId = messageInfo[0];
			String title = messageInfo[1];
			MessageType type = MessageType.content;
			MessageSubType subType = MessageSubType.threadMsg;
			MessageAuthorization userAutoAuthorization = MessageAuthorization.valueOf(authorization);
			GuiMessage newGuiMessage = new GuiMessage(body, title, type, subType, userAutoAuthorization,messageId);
			serverMessages.add(newGuiMessage);
		}
		
	}

	private void buildRegisterMsg(Vector<GuiMessage> serverMessages,
			String[] messageWithoutTopic, String messageTopic) {
		String authorization = messageWithoutTopic[0];
		System.out.println(messageTopic);
		String title = messageTopic;
		String body = "";
		for(int i = 1 ; i < messageWithoutTopic.length-1;i++)
			body+=messageWithoutTopic[i]+"\n";
		MessageType type = MessageType.ack;
		MessageSubType subType = MessageSubType.registerAck;
		MessageAuthorization userAutoAuthorization = MessageAuthorization.valueOf(authorization);
		GuiMessage newGuiMessage = new GuiMessage(body, title, type, subType, userAutoAuthorization);
		serverMessages.add(newGuiMessage);		
				
	}

	private void buildLoginMsg(Vector<GuiMessage> serverMessages,
			String[] messageWithoutTopic, String messageTopic) {
		String authorization = messageWithoutTopic[0];
		String title = messageTopic;
		String body = "";
		for(int i = 1 ; i < messageWithoutTopic.length-1;i++)
			body+=messageWithoutTopic[i]+"\n";
		MessageType type = MessageType.ack;
		MessageSubType subType = MessageSubType.loginAck;
		MessageAuthorization userAutoAuthorization = MessageAuthorization.valueOf(authorization);
		GuiMessage newGuiMessage = new GuiMessage(body, title, type, subType, userAutoAuthorization);
		serverMessages.add(newGuiMessage);		
		
	}



}
